#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

class Solution{
    public:
    //error 数组次序不能改变！！！
    int ans1(vector<int>& nums, int target){
        int length = nums.size();
        sort(nums.begin(), nums.end());
        vector<int> arr;
        for(int i = length-1; i >= 0; i--){
            arr.push_back(nums[i]);
            target = target - nums[i];
            if(target <= 0){
                return arr.size();
            }
        }
        return 0;
    }

    int ans2(vector<int>& nums, int target){
        int length = nums.size();
        int p_l = 0, p_r = 0;
        int sum = 0;
        int len = 0;
        int min_ans = length;
        while(p_r < length){
            sum = sum + nums[p_r];
            p_r++;
            len++;
            while(sum >= target){
                min_ans = min(len, min_ans);
                sum = sum - nums[p_l];
                p_l++;
                len--;
            }
        }
        if(sum < target && len == length){
            return 0;
        }else{
            return min_ans;
        }
    }


};

void test1(){
    Solution s;
    vector<int> nums = {2, 3, 1, 2, 4, 3};
    int target = 7;
    // int ret = s.ans1(nums, target);
    int ret = s.ans2(nums, target);
    cout << ret << endl;
}

void test2(){
    Solution s;
    vector<int> nums = {1,4,4};
    int target = 4;
    // int ret = s.ans1(nums, target);
    int ret = s.ans2(nums, target);
    cout << ret << endl;
}

void test3(){
    Solution s;
    vector<int> nums = {12,28,83,4,25,26,25,2,25,25,25,12};
    int target = 213;
    // int ret = s.ans1(nums, target);
    int ret = s.ans2(nums, target);
    cout << ret << endl;
}

void test4(){
    Solution s;
    vector<int> nums = {2,16,14,15};
    int target = 20;
    // int ret = s.ans1(nums, target);
    int ret = s.ans2(nums, target);
    cout << ret << endl;
}

int main(void){
    test1();
    test2();
    test3();
    test4();

    return 0;
}